function reduce(arr, reduceCallback, initialValue) {
    if (!Array.isArray(arr) || arr.length === 0 || typeof reduceCallback !== 'function')
        return [];
    else {
        let hasInitialValue = initialValue !== undefined;
        let value = hasInitialValue ? initialValue : arr[0];

        for(let i = hasInitialValue ? 0 : 1; i < arr.length; i++) {
            value = reduceCallback(value, arr[i], i, arr)
        }
        return value;
    }
};

const arr = [1,2,3,4,5];
console.log(reduce(arr,(pre,cur) => {
    pre.push(cur * 2);
    return pre;
}, []));
console.log(reduce(arr,(pre,cur) => pre * cur));